package org.next.tesigiulia.controllers;

import com.google.common.eventbus.Subscribe;
import gov.nasa.worldwind.geom.Position;
import org.next.tesigiulia.models.Area;
import org.next.tesigiulia.models.AreasModel;
import org.next.tesigiulia.utils.MyConstant;
import org.next.tesigiulia.utils.MyEventBus;
import org.next.tesigiulia.utils.MyTrigger;
import org.next.tesigiulia.views.AreaView;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Logger;

public class AreaController implements IAreaController {

    private AreaView areaView;
    private AreasModel areasModel;
    private Area currentArea;
    private Area tempArea;
    private int tempIndex;
    private String triggerBtn;
    private String triggerBtnAdd2;
    private double lat;
    private double lon;
    private Position position;

    public AreaController(AreasModel model, AreaView view, Area currentArea) {
        areasModel = model;
        areaView = view;
        this.currentArea = currentArea;
        areasModel.setPannelloInfoVisile(true);
        areasModel.setBtnAdd2Enabled(true);
        MyEventBus.getInstance().getEventBus().register(this);

    }

    @Override
    public void txtAreaChanged(String text) {
        areasModel.getCurrentArea().setAreaName(text);
        areasModel.setTxtAreaBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
    }

    @Override
    public void txtLatitudeChanged(String text) {
        try {
            Double b1 = (Double.valueOf(text));
            Double max = 90.0;
            Double min = -90.0;

            if (b1 >= min && b1 <= max) {

                areasModel.getCurrentArea().setLatitudine(b1);
                areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

            } else {
                areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.RED));
            }

        } catch (NumberFormatException e) {

        }
    }

    @Override
    public void txtLongitudineChanged(String text) {
        try {
            Double b = (Double.valueOf(text));
            Double max = 180.0;
            Double min = -180.0;

            if (b >= min && b <= max) {

                areasModel.getCurrentArea().setLongitudine(b);
                areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

            } else {
                areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.RED));
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void sldRadiusChanged(Integer radius) {
        areasModel.getCurrentArea().setRaggio(radius);
    }

    @Override
    public void btnClearClicked() {
        areasModel.setCurrentArea(new Area());
        areasModel.setTxtAreaBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
    }

    @Override
    public void btnAnnullaClicked() {

        disabilitaLbls();
        disabilitaTxtFieldsAndBar();

        areasModel.setBtnAddEnabled(false);
        areasModel.setBtnAnnullaEnabled(false);
        areasModel.setBtnClearEnabled(false);
        areasModel.setBtnApplyEnabled(false);
        areasModel.setBtnAdd2Enabled(true);
        areasModel.setTxtAreaBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        triggerBtnAdd2 = "";

        if (MyConstant.AREA_VIEW_BTN_MODIFICA.equals(triggerBtn)) {
            areasModel.setCurrentArea(new Area());
            areasModel.set(this.tempIndex, this.tempArea);
            areasModel.setJlistEnabled(true);
        }

        areasModel.setCurrentArea(new Area());
    }

    @Override
    public void btnAddClicked() {
        boolean check = true;
        boolean found = false;
        Area cArea = areasModel.getCurrentArea();
        check = check && (null != cArea.getAreaName()) && !cArea.getAreaName().isEmpty();
        check = check && (null != cArea.getLatitudine());
        check = check && (null != cArea.getLongitudine());

        for (int i = 0; i < areasModel.size(); i++) {
            Logger.getAnonymousLogger().info("");
            if ((cArea.getAreaName().equals(areasModel.get(i).getAreaName()))) {
                found = true;
                break;
            }
        }
        check &= !found;
        if (check) {

            disabilitaTxtFieldsAndBar();
            disabilitaLbls();

            areasModel.setBtnAnnullaEnabled(false);
            areasModel.setBtnClearEnabled(false);
            areasModel.setBtnAddEnabled(false);
            areasModel.setBtnModificaEnabled(false);
            areasModel.setBtnAdd2Enabled(true);
            areasModel.setPannelloInfoVisile(true);
            areasModel.setJlistEnabled(true);

            areasModel.add(areasModel.getCurrentArea());

            areasModel.setCurrentArea(new Area());
            triggerBtnAdd2 = "";


        } else {
            if ((null == cArea.getAreaName()) || cArea.getAreaName().isEmpty()) {
                areasModel.setTxtAreaBorderColorEnabled(BorderFactory.createLineBorder(Color.RED));
            }
            if (null == cArea.getLatitudine()) {
                areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.RED));
            }
            if (null == cArea.getLongitudine()) {
                areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.RED));
            }
        }
    }


    @Override
    public void btnModificaClicked(int index) {
        triggerBtn = MyConstant.AREA_VIEW_BTN_MODIFICA;

        areasModel.setBtnApplyEnabled(true);
        areasModel.setBtnAnnullaEnabled(true);
        areasModel.setBtnModificaEnabled(false);
        areasModel.setBtnAdd2Enabled(false);

        abilitaTxtFieldsAndBar();
        abilitaLbls();
        areasModel.setJlistEnabled(false);

        areasModel.setTxtAreaBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        Area area = areasModel.get(index);

        this.tempArea = new Area(area);
        this.tempIndex = index;

        areasModel.setCurrentArea(area);
    }

    @Override
    public void btnEliminaClicked(int index) {
        areasModel.remove(index);
        int size = areasModel.size();
        areasModel.setCurrentArea(new Area());
        areasModel.setBtnAnnullaEnabled(false);

        if (size == 0) {
            areasModel.setBtnModificaEnabled(false);
            areasModel.setBtnElininaEnabled(false);
        }
    }

    @Override
    public void btnNewClicked() {
        triggerBtnAdd2 = MyConstant.AREA_VIEW_BTN_ADD2;
        areasModel.setPannelloInfoVisile(true);

        areasModel.setBtnAddEnabled(true);
        areasModel.setBtnAnnullaEnabled(true);
        areasModel.setBtnClearEnabled(true);
        areasModel.setBtnAdd2Enabled(false);
        areasModel.setBtnElininaEnabled(false);
        areasModel.setBtnModificaEnabled(false);
        areasModel.setBtnApplyEnabled(false);

        abilitaTxtFieldsAndBar();
        abilitaLbls();

        areasModel.setJlistEnabled(false);

        areasModel.setTxtAreaBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        areasModel.setCurrentArea(new Area());

    }

    @Override
    public void btnApplyClicked() {
        areasModel.setCurrentArea(new Area());
        disabilitaLbls();
        disabilitaTxtFieldsAndBar();

        areasModel.setBtnAddEnabled(false);
        areasModel.setBtnAnnullaEnabled(false);
        areasModel.setBtnClearEnabled(false);
        areasModel.setBtnApplyEnabled(false);
        areasModel.setBtnAdd2Enabled(true);

        areasModel.setJlistEnabled(true);
    }

    @Override
    public void selectedIndexChanged(int index) {

        if (index == -1) {
            areasModel.setBtnElininaEnabled(false);
            areasModel.setBtnModificaEnabled(false);
            areasModel.setBtnAnnullaEnabled(false);

            areasModel.setCurrentArea(new Area());
        } else {
            areasModel.setBtnElininaEnabled(true);
            areasModel.setBtnModificaEnabled(true);
            //visualizza al click la lista
            Area area = areasModel.get(index);

            this.tempArea = new Area(area);
            this.tempIndex = index;

            areasModel.setCurrentArea(area);
            areasModel.set(this.tempIndex, this.tempArea);
        }
    }

    @Override
    public void frameClosed() {
        areasModel.setJframeVisible(false);


    }

    @Subscribe
    public void areaRequest(MyTrigger myTrigger) {
        if (myTrigger.getOrigin().equals(MyConstant.TITOLOFINESTRAWWJ)) {
            areasModel.setJframeVisible(true);
        }
    }

    @Subscribe
    public void handlePosition(Position position) {

        if (MyConstant.AREA_VIEW_BTN_ADD2.equals(triggerBtnAdd2) ||
                MyConstant.AREA_VIEW_BTN_MODIFICA.equals(triggerBtn)) {

            lat = position.getLatitude().getDegrees();
            lon = position.getLongitude().getDegrees();

            areasModel.setTxtLatitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
            areasModel.setTxtLongitudineBorderColorEnabled(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

            areasModel.getCurrentArea().setLatitudine(lat);
            areasModel.getCurrentArea().setLongitudine(lon);

            Area area = areasModel.getCurrentArea();
            areasModel.setCurrentArea(area);
        }
    }


    private void abilitaTxtFieldsAndBar() {
        areasModel.setTxtAreaEnabled(true);
        areasModel.setTxtLatitudeEnabled(true);
        areasModel.setTxtLongitudeEnabled(true);
        areasModel.setBarRaggioEnabled(true);
    }


    private void disabilitaTxtFieldsAndBar() {
        areasModel.setTxtAreaEnabled(false);
        areasModel.setTxtLatitudeEnabled(false);
        areasModel.setTxtLongitudeEnabled(false);
        areasModel.setBarRaggioEnabled(false);
    }

    public void disabilitaLbls() {
        areasModel.setLblAreaEnabled(false);
        areasModel.setLblLatitudeEnabled(false);
        areasModel.setLblLongitudeEnabled(false);
        areasModel.setLblRaggioEnabled(false);
    }

    public void abilitaLbls() {
        areasModel.setLblAreaEnabled(true);
        areasModel.setLblLatitudeEnabled(true);
        areasModel.setLblLongitudeEnabled(true);
        areasModel.setLblRaggioEnabled(true);
    }


}


